package com.starblink.store.manage.adapter

import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.util.FontUtil
import com.starblink.basic.ext.load
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.fragment.BrandVOF
import com.starblink.store.R
import com.starblink.store.databinding.ItemManageBrandBinding

class ItemBrandManageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleRes) {
    lateinit var merchat: BrandVOF

    val binding: ItemManageBrandBinding = ItemManageBrandBinding.inflate(inflater, this, true)

    companion object {
    }


    fun bindPost(
        merchat: BrandVOF,
        selects: MutableList<BrandVOF> = mutableListOf(),
        hasSelect: Boolean = true
    ) {
        this.merchat = merchat
        binding.ivSelect.visibleOrGone(hasSelect) {
            this.setImageResource(if (selects.contains(merchat)) R.mipmap.icon_store_selected else R.mipmap.icon_store_select)
        }

        binding.manage.tvShop.click {
            navigationTo(RoutePage.Brand.BRAND_DETAIL) {
                withString(
                    RoutePage.Brand.ARG_BRAND_ID,
                    merchat.brandId.toString()
                )
            }
        }

        binding.manage.ivBg.load(merchat.brandImg)
        binding.manage.title.text = merchat.name
        binding.manage.ivStore.load(merchat.logo ?: "", merchat.name)
        binding.manage.tvFollowers.text = "${merchat.subNums} ${CommUtils.getString(com.starblink.basic.style.R.string.following_2)}"
        binding.manage.ivDiscount.visibleOrGone(merchat.brandDiscount ?: 0 > 0) {}
        binding.manage.div.visibleOrGone(merchat.brandDiscount ?: 0 > 0) {}
        binding.manage.tvDiscount.visibleOrGone(merchat.brandDiscount ?: 0 > 0) {
            setBackgroundResource(com.starblink.android.common.R.drawable.tag_store_discount)
            buildSpannableString {
                addText("Up To ") {
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                        setTypeface(it)
                    }
                }
                addText("${merchat.brandDiscount}%") {
                    setScale(1.2f)
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_BOLD)?.let {
                        setTypeface(it)
                    }
                }
                addText(" Off") {
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                        setTypeface(it)
                    }
                }
            }
        }
    }
}